//
//  GameScene.swift
//  MySpriteKit
//
//  Created by haogaoming on 16/9/9.
//  Copyright (c) 2016年 郝高明. All rights reserved.
//

import SpriteKit

class GameScene: SKScene,ProtocolMainScene {
    
    lazy var panda = Panda()
    lazy var platformFactory = PlatformFactory()
    
    //移动速度
    var moveSpeed:CGFloat = 15
    var lastDis:CGFloat = 0.0
    
    
    override func didMoveToView(view: SKView) {
        backgroundColor = SKColor(colorLiteralRed: 113/255, green: 197/255, blue: 207/255, alpha: 1)
        panda.position = CGPointMake(200, 400)
        addChild(panda)
        
        //平台创建
        addChild(platformFactory)
        platformFactory.delegate = self
        platformFactory.sceneWidth = frame.size.width
        platformFactory.createPlatorm(3, x: 0, y: 200)
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if panda.status == .run {
            panda.jump()
        }else if panda.status == .jump{
            panda.roll()
        }
    }
    /**
     每一帧都会执行一次
     
     - parameter currentTime: 毫秒
     */
    override func update(currentTime: CFTimeInterval) {
        lastDis -= moveSpeed
        if lastDis <= 0 {
            print("生产新的平台")
            platformFactory.createPlatormRandom()
        }
        platformFactory.platformMove(moveSpeed)
    }
    
    func onGetData(dist:CGFloat) {
        lastDis = dist
    }
}

